import Cesium from '../Ces/Cesium'
import EditablePoint from './EditablePoint';
import We from '../Core/We'

class EditableHandler {
  WE: We
  _isEditing: boolean = false
  _editingPoint
  _tooltip

  _leftUpHandler
  _leftDownkHandler
  _rightClickHandler
  _mouseMoveHandler
  constructor(WE) {
    this.WE = WE;
  }

  get isEditing() {
    return !this.WE.viewer.scene.screenSpaceCameraController.enableInputs;
  }
  set isEditing(value) {
    this.WE.viewer.scene.screenSpaceCameraController.enableInputs = !value;
  }

  clearHighlight() {
    if (Cesium.defined(this._editingPoint)
      && !this._editingPoint.id.isDestroyed()) {
      this._editingPoint.id.offHighlight()
      this._editingPoint = undefined;
    }
  }

  onHighlight(feature) {
    this.clearHighlight()
    this._editingPoint = feature;
    this._editingPoint.id.onHighlight()
  }

  updatePosition(position) {
    if (Cesium.defined(this._editingPoint)) {
      if (!this._tooltip) {
        this._tooltip = this.WE.earthPinCollection.getToolTip()
      }
      this._editingPoint.id.updatePosition(position, this._tooltip)
    }
  }

  pickPosition(scene, movement) {
    let position;

    if (Cesium.defined(this._editingPoint) && Cesium.defined(this._editingPoint.id)
      && Cesium.defined(this._editingPoint.id.destPickId)) {
      const pickedFeature = scene.pick(movement.endPosition)

      if (Cesium.defined(pickedFeature) && pickedFeature.id === this._editingPoint.id.destPickId) {
        position = scene.pickPosition(movement.endPosition)
      }
    } else {
      position = scene.camera.pickEllipsoid(
        movement.endPosition,
        scene.globe.ellipsoid,
      )
    }

    return position;
  }

  createMouseMoveHandler(scene) {
    const that = this;
    const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas)

    handler.setInputAction((movement) => {
      if (that.isEditing) {
        const position = that.pickPosition(scene, movement)

        if (Cesium.defined(position)) {
          that.updatePosition(position)
        }
      } else {
        that.clearHighlight()
        const pickedFeature = scene.pick(movement.endPosition)

        if (Cesium.defined(pickedFeature)
          && pickedFeature.id instanceof EditablePoint
          && !pickedFeature.id.isPrivate) {
          that.onHighlight(pickedFeature)
        }
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    this._mouseMoveHandler = handler;
  }

  createLeftDownHandler(scene) {
    const that = this;
    const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas)

    handler.setInputAction((movement) => {
      if (Cesium.defined(that._tooltip)) {
        that._tooltip.visible = false;
      }

      const pickedFeature = scene.pick(movement.position)

      if (Cesium.defined(pickedFeature)
        && pickedFeature.id instanceof EditablePoint
        && !pickedFeature.id.isPrivate) {
        that.onHighlight(pickedFeature)
        that.isEditing = true;
      }
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN)
    this._leftDownkHandler = handler;
  }

  createLeftUpHandler(scene) {
    const that = this;
    const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas)

    handler.setInputAction(() => {
      that.isEditing = false;
      that.clearHighlight()
    }, Cesium.ScreenSpaceEventType.LEFT_UP)
    this._leftUpHandler = handler;
  }

  createRightClickHandler(scene) {
    const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas)

    handler.setInputAction((movement) => {
      const pickedFeature = scene.pick(movement.position)

      if (Cesium.defined(pickedFeature)
        && pickedFeature.id instanceof EditablePoint
        && !pickedFeature.id.isPrivate) {
        pickedFeature.id.destroy()
      }
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
    this._rightClickHandler = handler;
  }

  start() {
    const scene = this.WE.viewer.scene;

    this.createMouseMoveHandler(scene)
    this.createLeftDownHandler(scene)
    this.createLeftUpHandler(scene)
    this.createRightClickHandler(scene)
  }

  destroy() {
    this._leftUpHandler = this._leftUpHandler && this._leftUpHandler.destroy()
    this._leftDownkHandler = this._leftDownkHandler && this._leftDownkHandler.destroy()
    this._rightClickHandler = this._rightClickHandler && this._rightClickHandler.destroy()
    this._mouseMoveHandler = this._mouseMoveHandler && this._mouseMoveHandler.destroy()
  }

  isDestroyed() {
    return false
  }
}




export default EditableHandler;
